/*
 * ResultSet.java
 *
 * Created on 7 de abril de 2005, 15:35
 */

package jdbc_04;
import jdbc_01.Conexion;

public class ResultSetTypeRelAbsDemo {
    
    Conexion con;
    javax.swing.JTextArea jta;
    
    ResultSetTypeRelAbsDemo() {
        Conexion.driverOdbc();
        con = new Conexion("empleados", "empleados", "empleados");
        if (con.abrirConexion()) {
            verDatos();
            con.cerrarConexion();
        }
    }
    
    void verDatos(){
        jta = new javax.swing.JTextArea(10,40);
        try {
            java.sql.Statement stmt = con.getCon().createStatement(
                    java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, 
                    java.sql.ResultSet.CONCUR_READ_ONLY);

            java.sql.ResultSet rset = stmt.executeQuery("Select * from empleados");
            java.sql.ResultSetMetaData rsmd = rset.getMetaData();
            
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                jta.append(rsmd.getColumnName(i).toUpperCase());
                if (rsmd.getColumnName(i).equals("nacimiento")) jta.append("\t");
                jta.append("\t");
            }
            jta.append("\n");

            rset.absolute(2);
            leerDatos(rset, rsmd.getColumnCount());
            rset.relative(2);
            leerDatos(rset, rsmd.getColumnCount());
            rset.relative(-1);
            leerDatos(rset, rsmd.getColumnCount());
            rset.absolute(1);
            leerDatos(rset, rsmd.getColumnCount()); 
            rset.absolute(-1);
            leerDatos(rset, rsmd.getColumnCount());             

            rset.close();    
            stmt.close();
        } catch (java.sql.SQLException e) {
            jta.setText(e.getMessage());
        } finally {
            javax.swing.JOptionPane.showMessageDialog(null, 
                                new javax.swing.JScrollPane(jta));
        }
    }
    
    void leerDatos(java.sql.ResultSet rset, int cols){
        try {
            for ( int i = 1; i <= cols; i++) {
                Object o = rset.getObject(i);
                if ( o != null )
                    jta.append(o.toString().trim());
                else 
                    jta.append("\t");
                jta.append("\t");
            }
            jta.append("\n");

        } catch (java.sql.SQLException e) {
            jta.append("\n" + e.getMessage() + "\n");
        }
    }    
    
    public static void main(String[] args) {
        new ResultSetTypeRelAbsDemo();
        System.exit(0);
    }
    
}